<?php
/**
 * @author spenceryg
 * @date 2022/12/11
 * @time 12:19 上午
 * @focus love & life
 */


namespace app\auto\controller;


use app\auto\lib\ControllerGenerator;
use app\auto\lib\ViewGenerator;
use app\common\model\Hospital;
use PHPExcel;
use PhpOffice\PhpSpreadsheet\Cell\DataValidation;
use PhpOffice\PhpSpreadsheet\Document\Security;
use pQuery;
use think\facade\Db;
use think\facade\View;

class Test extends \app\BaseController
{
    public function testCode()
    {
        $query = (new \app\common\model\Hospital)->with([
            'department' => [
                'doctor' => [
                    'disease' => [
                        'bodyParts' => [

                        ],
                    ],
                ],
            ],
            'city' => [
                'province' => [

                ],
            ],
        ])
            ->join('db_department', 'db_department.hospital_id = db_hospital.id')
            ->join('db_doctor', 'db_doctor.department_id = db_department.id');

        if(input('doctor_name')){
            $query->where('db_doctor.name', 'like', '%'. input('doctor_name') .'%');
        }
        $query->where('db_hospital.national_ranking', '>', '2');

        $query->field('db_hospital.*,db_hospital.name as hospital_name, db_hospital.national_ranking as hospital_national_ranking, db_hospital.brief_introduction as hospital_brief_introduction, db_doctor.name as doctor_name');
        $data = $query->select();
        dump($data->toArray());
    }

    public function form()
    {
        return View::fetch();
    }


    public function pQuery()
    {
        $html = '<div class="container">
  <div class="inner verb" id="aa">Hello</div>
  <div class="inner adj" id="bb">Cruel</div>
  <div class="inner obj">World</div>
</div>';

        $dom = pQuery::parseStr($html);
        $dom->query('#aa')->before('<a href="https://www.baidu.com">a</a>');
        $dom->query('#bb')->html('asdfasdf');
        echo $dom->html();
    }


    public function input(){
        $html = ViewGenerator::input([]);
        dump($html) ;
        return $html;
    }


    public function getHtml()
    {
        $html = file_get_contents('../app/auto/template/add.html');
        $dom = pQuery::parseStr($html);

        $fields = \app\auto\model\Field::where('table_id', 1)->select();
        $fields_html = '<div class="layui-form-item">';
        for($i=0; $i < 4; $i++){
            foreach ($fields as $field) {
                $field['mid'] = $field['id'];
                $field['is_inline'] = 1;
                if(!$field['is_sys']) {
                    $fields_html .= ViewGenerator::input($field,true, true);
                }
            }
        }


        $fields_html .= '</div>';



        $dom->query("#content-wrapper")->html($fields_html);
//        dump($dom->html());
        echo  $dom->html();
    }


    public function generateController()
    {
        ControllerGenerator::generate();
        return 'success';
    }


    public function deleteController(){
        ControllerGenerator::delete();
        return 'success';
    }

    public function export()
    {

        $data = [
            ['姓名', '编号', '性别'],
            ['姓名1', '1', '男'],
            ['姓名2', '2', '女'],
            ['姓名3', '3', '男'],
        ];
// create a new PhpSpreadsheet object
        $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();

// set the active sheet index to the first sheet, so Excel opens this as the first sheet
        $spreadsheet->setActiveSheetIndex(0);

// get the active sheet
        $sheet = $spreadsheet->getActiveSheet();

// set the sheet title
        $sheet->setTitle('Sheet1');

        // set the filter values for cells A1:C1
//        $sheet->setAutoFilter('A1:C1');



        $validate = new DataValidation();
        $validate->setType(DataValidation::TYPE_LIST)
            ->setFormula1('"A,B,C,A1,A2"')
            ->setAllowBlank(true)
            ->setShowDropDown(true)
            ->setShowErrorMessage(true);
        // set the cell range to allow selection from a list
        $sheet->setDataValidation('D1:D100', $validate);




// loop through the data and write it to the sheet
        $rowNumber = 1; // start at row 1
        foreach ($data as $row) {
            $col = 'A'; // start at column A
            foreach ($row as $cell) {
                $sheet->setCellValue($col . $rowNumber, $cell);
                $col++;
            }
            $rowNumber++;
        }

// create the writer
        $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');

// set the HTTP headers
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename="exported-array.xlsx"');
        header('Cache-Control: max-age=0');

// save the file to the browser
        $writer->save('php://output');
        exit;




    }


    public function root()
    {
//        return app()->getRootPath();
        $disabled_functions = ini_get('safe_mode');
        dump($disabled_functions);


    }

    public function decrypt()
    {
        dump(decrypt('nHAD0EoP34FVo/Nmh3XunA=='));
    }

    public function fields()
    {
        $res = Db::name('hospital')->field('name')->field('id')->select();
        dump($res);
    }


    public function substr()
    {
        dump(substr('asdfasdf==s==', 0 , -5));
        return substr('asdfasdf==s==', -5);
    }



}
